/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2017 All Rights Reserved.
 */
package com.maple.retry.retrytask.util;


import com.maple.retry.retrytask.ScheduleStrategy;

import java.util.Arrays;
import java.util.List;

/**
 * @author maple
 * @version $Id: TimeUtil.java, v 0.1 2020/12/1 下午7:57 maple Exp $
 */
public class TimeUtil {

    /**
     * Parse string of time interval.
     * <p>
     * Format:
     * <pre>
     *  time:[Number][Unit],[Number][Unit]...[Number][Unit]
     * </pre>
     * <ul>
     * <li>[Number]: 0 and positive integer, default to 0 if illegal
     * <li>[Unit]: possible value: y(year), M(month), w(week), d(day), h(hour), m(minute), s(second) . default <i>s</i>
     * </ul>
     *
     * @param originalIntervalStr string of time interval
     * @return sequence of time in millisecond . Not <tt>null</tt>.
     */
    public static void main(String[] args) {

        String strRetryIntervals = "time:2s~20s";

        ScheduleStrategy scheduleStrategy = new ScheduleStrategy(strRetryIntervals);

    }

    /**
     * symbol :
     */
    public static final String COLON = ":";

    /**
     * symbol ,
     */
    public static final String COMMA = ",";

    /**
     * symbol ~
     */
    public static final String TILDE = "~";

    /**
     * time unit:  year
     */
    public static final String YEAR = "y";

    /**
     * time unit: week
     */
    public static final String WEEK = "w";

    /**
     * time unit: month
     */
    public static final String MONTH = "M";

    /**
     * time unit: day
     */
    public static final String DAY = "d";

    /**
     * time unit: hour
     */
    public static final String HOUR = "h";

    /**
     * time unit: minute
     */
    public static final String MINUTE = "m";

    /**
     * time unit: second
     */
    public static final String SECOND = "s";

    /**
     * 支持的时间单位
     */
    public static final List<String> validTimeUnit = Arrays.asList(YEAR, WEEK, MONTH, DAY, HOUR,
            MINUTE, SECOND);

    /**
     * 将时间字符串解析为对应的毫秒数
     *
     * <p>
     * 比如: 1s -> 1000, 1m -> 60000
     * </p>
     *
     * @param timeStr 时间字符串
     * @param unit    单位
     * @return 对应的毫秒数
     */
    public static long parseTime(String timeStr, String unit) {

        long multipiler = 1000L;
        if (YEAR.equals(unit)) {
            multipiler = 365L * 24L * 60L * 60L * 1000L;
        } else if (MONTH.equals(unit)) {
            multipiler = 30L * 24L * 60L * 60L * 1000L;
        } else if (WEEK.equals(unit)) {
            multipiler = 7L * 24L * 60L * 60L * 1000L;
        } else if (DAY.equals(unit)) {
            multipiler = 24L * 60L * 60L * 1000L;
        } else if (HOUR.equals(unit)) {
            multipiler = 60L * 60L * 1000L;
        } else if (MINUTE.equals(unit)) {
            multipiler = 60L * 1000L;
        } else if (SECOND.equals(unit)) {
            multipiler = 1000L;
        }

        long time = 0L;
        try {
            time = Long.valueOf(timeStr);
            if (time < 0) {
                time = 0L;
            }
        } catch (Exception e) {
            time = 0L;
        }

        return time * multipiler;
    }
}
